package com.luhaoran.hrm.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luhaoran.hrm.entity.Department;
import com.luhaoran.hrm.exception.MyJsonValidationException;
import com.luhaoran.hrm.mapper.IDepartmentMapper;
import com.luhaoran.hrm.service.IDepartmentService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author haoranlu
 * @project IntelliJ IDEA
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<IDepartmentMapper, Department> implements IDepartmentService {

    @Override
    public List<Department> getList() {
        QueryWrapper<Department> departmentQueryWrapper = new QueryWrapper<>();
        departmentQueryWrapper.orderByDesc("sort");
        return list(departmentQueryWrapper);
    }

    @Override
    public Integer saveDepartment(Department department) {
        QueryWrapper<Department> departmentQueryWrapper = new QueryWrapper<>();
        departmentQueryWrapper.eq("name",department.getName())
                .ne("id",department.getId());
        if (count(departmentQueryWrapper) > 0){
            throw new MyJsonValidationException("部门已存在");
        }
        saveOrUpdate(department);
        return department.getId();
    }
}
